{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "--------\n",
    "\n",
    "* 本学习记录人：许智超\n",
    "* 学习时间：2021-11-30\n",
    "* week14\n",
    "* 参考的文档：\n",
    "> 1. [python官方文档](https://python.org/doc)\n",
    "> 2. [许老师博客](https://cnblogs.com/xuzhichao)\n",
    "\n",
    "------\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 本周重点知识\n",
    "\n",
    "## 1. 初始字典\n",
    "\n",
    "## 2. 字典的一般创建方法\n",
    "\n",
    "## 3. 字典的操作\n",
    "\n",
    "## 4. 实践\n",
    "------"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 1. 初识[字典](https://docs.python.org/3/tutorial/datastructures.html#dictionaries)\n",
    "\n",
    "\n",
    "* Another useful data type built into Python is the dictionary (see Mapping Types — dict) **<font style=\"color:red\">映射数据类型</font>**\n",
    "* 对比序列数据 VS 映射数据\n",
    ">> 1. 序列数据 index 是 从0开始，每次加一的 数值作为索引值 [88,87,90]\n",
    ">> 2. 映射数据 index 是 **关键字**（不可变数据，例如字符串，数字，元组）\n",
    ">> 3. 序列数据是 **有顺序的**\n",
    ">> 4. 映射数据是 **无序的**\n",
    ">> 5. 序列数据 index 是不写在数据值中的，不显示的\n",
    ">> 6. 映射数据 index 作为 关键字（key）写在数据结构中 ： {key:value} 这种形式通常被人们称为 **键值对（key: value pairs）**\n",
    "\n",
    "* 总结字典的性质：\n",
    "> 1. 字典的key 是唯一的 (唯一性)\n",
    "> 2. 字典是没有顺序的（无序性）\n",
    "> 3. 字典的key是不可变的(不变性)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 初识字典\n",
    "tel = {'jack': 4098, 'sape': 4139}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'jack': 4098, 'sape': 4139}"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tel"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2.字典的一般创建方法\n",
    "\n",
    "> 1. 按照字典的数据结构直接创建\n",
    ">> * dict_name = {key1:value1 , key2:value2 , key3:value3 , ...}\n",
    "> 2. 使用dict()方法创建字典\n",
    ">> * dict_name = dict(key1=value1 , key2=value2 , key3=value3 , ...)\n",
<<<<<<< HEAD
    "> 3. 使用字典新增的方式创建字典（常用）\n",
=======
    "> 3. 使用字典新增的方式创建字典\n",
>>>>>>> 20d168b4f720b271344a49bf3b4716da6fe3791b
    ">> * dict_name[key1] = value1 , dict_name[key2] = value2 , dict_name[key3] = value3 ,..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# **注意： 冒号和逗号都是英文的**\n",
    "stud_info = {\n",
    "    \"2100001\":\"Alex\",\n",
    "    \"2100002\":\"Jack\",\n",
    "    \"2100003\":\"Lihua\"\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'dict'>\n",
      "{'2100001': 'Alex', '2100002': 'Jack', '2100003': 'Lihua'}\n"
     ]
    }
   ],
   "source": [
    "print(type(stud_info))\n",
    "print(stud_info)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Jack'"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stud_info[\"2100002\"]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "*  练习查询\n",
    "https://httpbin.org/get\n",
    "```\n",
    "{\n",
    "  \"args\": {}, \n",
    "  \"headers\": {\n",
    "    \"Accept\": \"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9\", \n",
    "    \"Accept-Encoding\": \"gzip, deflate, br\", \n",
    "    \"Accept-Language\": \"zh-CN,zh;q=0.9\", \n",
    "    \"Host\": \"httpbin.org\", \n",
    "    \"Sec-Ch-Ua\": \"\\\"Chromium\\\";v=\\\"92\\\", \\\" Not A;Brand\\\";v=\\\"99\\\", \\\"Google Chrome\\\";v=\\\"92\\\"\", \n",
    "    \"Sec-Ch-Ua-Mobile\": \"?0\", \n",
    "    \"Sec-Fetch-Dest\": \"document\", \n",
    "    \"Sec-Fetch-Mode\": \"navigate\", \n",
    "    \"Sec-Fetch-Site\": \"none\", \n",
    "    \"Sec-Fetch-User\": \"?1\", \n",
    "    \"Upgrade-Insecure-Requests\": \"1\", \n",
    "    \"User-Agent\": \"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36\", \n",
    "    \"X-Amzn-Trace-Id\": \"Root=1-61a58fbf-7314033011179e850449460a\"\n",
    "  }, \n",
    "  \"origin\": \"210.21.79.254\", \n",
    "  \"url\": \"https://httpbin.org/get\"\n",
    "}\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "get_dict = {\n",
    "  \"args\": {}, \n",
    "  \"headers\": {\n",
    "    \"Accept\": \"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9\", \n",
    "    \"Accept-Encoding\": \"gzip, deflate, br\", \n",
    "    \"Accept-Language\": \"zh-CN,zh;q=0.9\", \n",
    "    \"Host\": \"httpbin.org\", \n",
    "    \"Sec-Ch-Ua\": \"\\\"Chromium\\\";v=\\\"92\\\", \\\" Not A;Brand\\\";v=\\\"99\\\", \\\"Google Chrome\\\";v=\\\"92\\\"\", \n",
    "    \"Sec-Ch-Ua-Mobile\": \"?0\", \n",
    "    \"Sec-Fetch-Dest\": \"document\", \n",
    "    \"Sec-Fetch-Mode\": \"navigate\", \n",
    "    \"Sec-Fetch-Site\": \"none\", \n",
    "    \"Sec-Fetch-User\": \"?1\", \n",
    "    \"Upgrade-Insecure-Requests\": \"1\", \n",
    "    \"User-Agent\": \"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36\", \n",
    "    \"X-Amzn-Trace-Id\": \"Root=1-61a58fbf-7314033011179e850449460a\"\n",
    "  }, \n",
    "  \"origin\": \"210.21.79.254\", \n",
    "  \"url\": \"https://httpbin.org/get\"\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'zh-CN,zh;q=0.9'"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "get_dict[\"headers\"]['Accept-Language']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 2. 使用dict()方法创建字典(局限性： key 是以 变量名的方式进行赋值的)\n",
    "stud_info2 = dict(\n",
    "     name1 = \"Alex\",\n",
    "     name2 = \"Jack\",\n",
    "     name3 = \"Lihua\"\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'name1': 'Alex', 'name2': 'Jack', 'name3': 'Lihua'}"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stud_info2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 3. 使用字典新增的方式创建字典\n",
    "stud_info3 = {}\n",
    "stud_info3[\"2100001\"] = \"Alex\"\n",
    "stud_info3[\"2100002\"] = \"Jack\"\n",
    "stud_info3[\"2100003\"] = \"Lihua\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'2100001': 'Alex', '2100002': 'Jack', '2100003': 'Lihua'}"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stud_info3"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 3. 字典的操作\n",
    "\n",
    "> 1. 字典的增删改查\n",
    ">> 1. 字典的查询\n",
    ">> 2. 字典的增加\n",
    ">> 3. 字典的修改\n",
    ">> 4. 字典的删除\n",
    "> 2. 字典的一般方法"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> 1. 查询"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2100004不存在\n"
     ]
    }
   ],
   "source": [
    "# 1.字典的查询 方法1\n",
    "if '2100004' in stud_info3:\n",
    "    print(stud_info3['2100004'])\n",
    "else:\n",
    "    print('2100004'+\"不存在\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "ename": "KeyError",
     "evalue": "'2100004'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-24-4943be9953aa>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mstud_info3\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'2100004'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m: '2100004'"
     ]
    }
   ],
   "source": [
    "# 如果查询不存在： KeyError: '2100004'\n",
    "print(stud_info3['2100004'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "None\n"
     ]
    }
   ],
   "source": [
    "# 1.字典的查询 方法2 get()\n",
    "print(stud_info3.get('2100004'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> 2. 字典的增加\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "None\n"
     ]
    }
   ],
   "source": [
    "print(stud_info3.get('2100004'))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [],
   "source": [
    "if stud_info3.get('2100004')== None:\n",
    "    stud_info3['2100004'] = \"Marry\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'2100001': 'Alex', '2100002': 'Jack', '2100003': 'Lihua', '2100004': 'Marry'}"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stud_info3"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> 3. 字典的改变\n",
    ">> 1. dict_name[key]=value\n",
    ">> 2. 注意：如果字典的key不存在，则新增，如果key存在，则修改"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [],
   "source": [
    "if '2100004' in stud_info3:\n",
    "    stud_info3['2100004'] = \"Xiaoming\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'2100001': 'Alex',\n",
       " '2100002': 'Jack',\n",
       " '2100003': 'Lihua',\n",
       " '2100004': 'Xiaoming'}"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stud_info3"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> 4. 字典的删除\n",
    ">> 1. pop(): 指定key删除\n",
    ">>> 1. D.pop(k[,d])\n",
    ">>> 2. remove **specified key**（指定的key关键字） and return the **corresponding value**（返回删除的value值）\n",
    ">> 2. popitem()：随机删除"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'2100001': 'Alex',\n",
       " '2100002': 'Jack',\n",
       " '2100003': 'Lihua',\n",
       " '2100004': 'Xiaoming'}"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stud_info3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function pop:\n",
      "\n",
      "pop(...) method of builtins.dict instance\n",
      "    D.pop(k[,d]) -> v, remove specified key and return the corresponding value.\n",
      "    If key is not found, d is returned if given, otherwise KeyError is raised\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(stud_info3.pop)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Lihua'"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 因为字典是无序的，所以没有最后一个元素，pop指定关键字删除\n",
    "stud_info3.pop('2100003')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'2100001': 'Alex', '2100002': 'Jack', '2100004': 'Xiaoming'}"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stud_info3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function popitem:\n",
      "\n",
      "popitem(...) method of builtins.dict instance\n",
      "    D.popitem() -> (k, v), remove and return some (key, value) pair as a\n",
      "    2-tuple; but raise KeyError if D is empty.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(stud_info3.popitem)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('2100004', 'Xiaoming')"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stud_info3.popitem()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
<<<<<<< HEAD
   "version": "3.6.5"
=======
   "version": "3.8.3"
>>>>>>> 20d168b4f720b271344a49bf3b4716da6fe3791b
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
